<template>
  <el-dialog title="添加配置" :visible.sync="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" :rules="formRules" :label-width="formLabelWidth">
      <el-form-item label="参数名称" prop="configName"><el-input v-model="form.configName" placeholder="请输入参数名称"></el-input></el-form-item>
      <el-form-item label="参数键名" prop="configKey"><el-input v-model="form.configKey" placeholder="请输入参数键名"></el-input></el-form-item>
      <el-form-item label="参数键值" prop="configValue"><el-input type="textarea" v-model="form.configValue" placeholder="请输入参数键名" :autosize="{ minRows: 2, maxRows: 8}"></el-input></el-form-item>
      <el-form-item label="系统内置" prop="configType">
          <el-radio-group v-model="form.configType">
            <el-radio label="Y">是</el-radio>
            <el-radio label="N">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
import SysConfigApi from '@/api/sysconfig.js';
export default {
  data() {
    return {
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      loading: false,
      /*是否显示*/
      dialogVisible: false,
      /*form表单对象*/
      form: {
        configName: '',
        configKey:'',
        configValue:'',
        configType:'Y',
        remark:''
      },
      /*form验证*/
      formRules: {
        configName: [
          {
            required: true,
            message: ' ',
            trigger: 'blur'
          }
        ],
        configKey: [
          {
            required: true,
            message: ' ',
            trigger: 'blur'
          }
        ],
        configValue: [
          {
            required: true,
            message: ' ',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  props: ['open'],
  watch: {
    open: function(n, o) {
      if (n != o) {
        this.dialogVisible = this.open;
      }
    }
  },
  created() {

  },
  methods: {
    /*添加*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate(valid => {
        if(valid){
          self.loading = true;
          let params = Object.assign({},self.form);
          SysConfigApi.addConfig(params, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: '恭喜你，添加成功',
                type: 'success'
              });
              self.dialogFormVisible(true);
            })
            .catch(error => {
              self.loading = false;
            });
        }
      })
    },

    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('close', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('close', {
          type: 'error',
          openDialog: false
        });
      }
    }
  }
};
</script>

<style></style>
